package root.electronclass.electronclass.other

import android.annotation.SuppressLint
import android.util.Log
import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.lifecycle.coroutineScope
import root.electronclass.electronclass.common.base.ViewBindingActivity
import root.electronclass.electronclass.common.util.DateUtils
import root.electronclass.electronclass.databinding.FragmentFullNoticeWebBinding
import root.electronclass.electronclass.ext.CoroutineExceptionHandler
import root.electronclass.electronclass.title
import root.electronclass.electronclass.mvp.rest.RestManager
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.util.*


class FullNoticeWebActivity : ViewBindingActivity<FragmentFullNoticeWebBinding>() {
    override fun init() {
        loadUrl()
    }


    private fun close(time: String) {
        val endTime: Date = DateUtils.stringToDate(time)
        val newDate: Date = Calendar.getInstance().time
        val time = endTime.time - newDate.time
        Log.i("onFullScreen", "time:$time")
        if (time <= 0){
            finish()
        }else {
            lifecycle.coroutineScope.launch(CoroutineExceptionHandler()) {
                delay(time)
                finish()
            }
        }
    }

    @SuppressLint("WrongConstant")
    private fun loadUrl(url: String? = intent?.getStringExtra("url")?.let { it + "${if (it.contains("?")) "&" else "?"}token=${RestManager.getInstance().TOKEN}" }) {
        setWeb(url ?: return)
        binding.webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null)
        intent?.getStringExtra("endTime")?.let { close(it) }
    }

    private fun setWeb(url: String) {
        Log.i("webActivity--url:", url)
        val webView = binding.webView
        //得到webview设置
        val webSettings = webView.settings
        //允许使用javascript
        webSettings.javaScriptEnabled = true
        webSettings.domStorageEnabled = true
        webSettings.allowFileAccess = true
        webSettings.databaseEnabled = true
        webSettings.setSupportZoom(false)
        webSettings.builtInZoomControls = true
        webSettings.useWideViewPort = true
        webView.loadUrl(url)
        webView.webChromeClient = object : WebChromeClient() {
            override fun onReceivedTitle(view: WebView?, title: String?) {
                binding.top.title(title ?: "")
            }
        }
        webView.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
                //  重写此方法表明点击网页里面的链接还是在当前的webview里跳转，不跳到浏览器那边
                Log.i("WebViewClient:", url)
                view.loadUrl(url)
                return true
            }

            override fun onPageFinished(view: WebView, url: String) {
                super.onPageFinished(view, url)
            }

        }


        val displayMetrics = resources.displayMetrics
        displayMetrics.scaledDensity = 1f
        displayMetrics.density = 1f
    }

}